package com.bgs.travelapp.card.mapper;

import com.bgs.travelapp.card.entity.CardRecordEntity;
import com.bgs.travelapp.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CardRecordMapper extends BaseMapper<CardRecordEntity> {
    @Select("select * from buy_card_record where user_id=#{userId} and expired_id=#{expiredId} and card_id =#{cardId}")
    @Results(
            {@Result(property = "cardName", column = "card_name"),
                    @Result(property = "purchaseMonth", column = "purchase_month"),
                    @Result(property = "createTime", column = "create_time"),
                    @Result(property = "expirationTime", column = "expiration_time"),
            }
    )
    List<CardRecordEntity> selectexist(Integer userId, Integer expiredId, Integer cardId);

    @Select("select * from buy_card_record where user_id=#{userId}  and card_id =#{cardId} order by id desc limit 1")
    @Results(
            {@Result(property = "cardName", column = "card_name"),
                    @Result(property = "purchaseMonth", column = "purchase_month"),
                    @Result(property = "createTime", column = "create_time"),
                    @Result(property = "expirationTime", column = "expiration_time"),
                    @Result(property = "expiredId", column = "expired_id"),
            }
    )
    List<CardRecordEntity> getMemberOverdue(Integer userId, Integer cardId);

    @Select("select distinct(card_id) from buy_card_record where user_id=#{userId} and expired_id =0")
    @Results(
            {@Result(property = "cardId", column = "card_id")}
    )
    List<CardRecordEntity> getUserCard(Integer userId);

    @Select("SELECT b.id,b.card_name,b.card_id,b.purchase_month,b.card_price,b.create_time,b.expiration_time," +
            "c.cover_id FROM buy_card_record b ,card c where b.card_id=c.id and b.user_id=#{userId} and b.card_id=#{cardId} order by id desc limit 1")
    @Results(
            {@Result(property = "cardName", column = "card_name"),
                    @Result(property = "purchaseMonth", column = "purchase_month"),
                    @Result(property = "createTime", column = "create_time"),
                    @Result(property = "expirationTime", column = "expiration_time"),
                    @Result(property = "expiredId", column = "expired_id"),
                    @Result(property = "cardId", column = "card_id"),
                    @Result(property = "cardEntity.coverId", column = "cover_id"),
            }
    )
    CardRecordEntity getCardExpiration(Integer userId, Integer cardId);
}
